#include <mutex>

#include "rule_project.h"

namespace Elements
{

RuleProject::RuleProject(const RuleProject& other)
    : Element(other)
    , m_ruleId(other.m_ruleId)
    , m_projectId(other.m_projectId)
    , m_permissions(other.m_permissions)
{
}

bool RuleProject::isSame(Element* const other) const
{
    const auto otherElement = dynamic_cast<RuleProject const*>(other);
    if (nullptr == otherElement)
        return false;

    return bool(
        m_id == otherElement->m_id
        && m_ruleId == otherElement->m_ruleId
        && m_projectId == otherElement->m_projectId
        && m_permissions == otherElement->m_permissions
    );
}

Common::Json RuleProject::toJson() const
{
    std::shared_lock locker(m_lock);
    Common::Json result;
    result["id"] = m_id;
    result["elementType"] = unsigned(elementType());
    result["ruleId"] = m_ruleId;
    result["projectId"] = m_projectId;
    result["permissions"] = unsigned(m_permissions);
    return result;
}

void RuleProject::fromJson(const Common::Json& json)
{
    std::shared_lock locker(m_lock);
    m_id = json["id"].get<unsigned>();
    m_ruleId = json["ruleId"].get<unsigned>();
    m_projectId = json["projectId"].get<unsigned>();
    m_permissions = Permissions(json["permissions"].get<unsigned>());
}

Element* RuleProject::clone() const
{
    return new RuleProject(*this);
}

ElementType RuleProject::elementType() const
{
    return ElementType::RuleProject;
}

unsigned RuleProject::ruleId() const
{
    std::shared_lock locker(m_lock);
    return m_ruleId;
}

void RuleProject::setRuleId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_ruleId = value;
}

unsigned RuleProject::projectId() const
{
    std::shared_lock locker(m_lock);
    return m_projectId;
}

void RuleProject::setProjectId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_projectId = value;
}

Permissions RuleProject::permissions() const
{
    std::shared_lock locker(m_lock);
    return m_permissions;
}

void RuleProject::setPermissions(const Permissions& value)
{
    std::unique_lock locker(m_lock);
    m_permissions = value;
}

} // namespace Elements
